//
//  VEHomeBaseViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/6.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEHomeBaseViewController.h"
#import "VEMainBaseViewController.h"
#import "VEMainOtherViewController.h"
#import "VEHomeCustomNavigationView.h"
#import "SPPageMenu.h"
#define kPageMenuH 40
@interface VEHomeBaseViewController ()<SPPageMenuDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) SPPageMenu *pageMenu;
@property (nonatomic, strong) NSArray *musicCategories;
@end

@implementation VEHomeBaseViewController


- (NSArray *)musicCategories {
    if (!_musicCategories) {
//        _musicCategories = @[@"关注",@"推荐",@"财经号",@"币事",@"发展",@"新手阅读"];
        _musicCategories = @[@"新手阅读",@"推荐",@"行情"];
    }
    return _musicCategories;
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];

}


- (void)viewDidLoad {
    [super viewDidLoad];
   

    
    for (NSInteger i = 0; i < self.musicCategories.count; i++) {
        if (i == 0) {
            [self addChildViewController:[[VEMainBaseViewController alloc] init]];
        }else{
            [self addChildViewController:[[VEMainOtherViewController alloc] init]];
        }
        
    }
    
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageMenu];
    // 先将第一个子控制的view添加到scrollView上去
    [self.scrollView addSubview:self.childViewControllers[1].view];
    [self setupCustomNav];
}

- (void)setupCustomNav{
    VEHomeCustomNavigationView *nav = [[VEHomeCustomNavigationView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, Height_NavBar)];
    [self.view addSubview:nav];
}


#pragma mark - Datasource & Delegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (!self.childViewControllers.count) { return;}
    // 如果上一次点击的button下标与当前点击的buton下标之差大于等于2,说明跨界面移动了,此时不动画.
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * toIndex, 0) animated:NO];
    } else {
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * toIndex, 0) animated:YES];
    }
    if (toIndex == 0) {
        VEMainBaseViewController *targetViewController = self.childViewControllers[toIndex];
        // 如果已经加载过，就不再加载
        if ([targetViewController isViewLoaded]) return;
        targetViewController.indexType = toIndex;
        targetViewController.view.frame = CGRectMake(kScreenWidth*toIndex, 0, kScreenWidth, self.scrollView.height);
        
        [self.scrollView addSubview:targetViewController.view];
    }else{
        VEMainOtherViewController *targetViewController = self.childViewControllers[toIndex];
        // 如果已经加载过，就不再加载
        if ([targetViewController isViewLoaded]) return;
        targetViewController.indexType = toIndex;
        targetViewController.view.frame = CGRectMake(kScreenWidth*toIndex, 0, kScreenWidth, self.scrollView.height);
        
        [self.scrollView addSubview:targetViewController.view];
    }
   
    
}


- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, kPageMenuH+Height_NavBar, kScreenWidth, kScreenHeight-Height_TabBar_Space-Height_NavBar - kPageMenuH);
        
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(kScreenWidth*self.musicCategories.count, 0);
        _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
    return _scrollView;
}
- (SPPageMenu *)pageMenu {
    
    if (!_pageMenu) {
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, Height_NavBar, kScreenWidth, kPageMenuH) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
        [_pageMenu setItems:self.musicCategories selectedItemIndex:1];
        _pageMenu.delegate = self;
        _pageMenu.itemTitleFont = [UIFont systemFontOfSize:14];
        _pageMenu.selectedItemTitleColor =  kCommonRedColor;
        _pageMenu.unSelectedItemTitleColor = [UIColor colorWithHexString:kDetailTextBlackColor];
        _pageMenu.tracker.backgroundColor =kCommonRedColor;
        _pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
        _pageMenu.bridgeScrollView = self.scrollView;
        
    }
    return _pageMenu;
}
@end
